﻿using System;
using System.Collections.Generic;
using System.Linq;
using Cms.Utilities;

namespace Cms.Domain
{
    public class User
    {
        public User()
        {
            Created = DateTimeProvider.Current.Now;
            ForceLogout = false;
            Memberships = new List<Membership>();
        }

        public virtual int ID { get; set; }

        public virtual string Username { get; set; }

        public virtual string Password { get; set; }

        public virtual string Email { get; set; }

        public virtual string Firstname { get; set; }

        public virtual string Lastname { get; set; }

        public virtual string Phone { get; set; }

        public virtual Address DeliveryAddress { get; set; }

        public virtual Address InvoiceAddress { get; set; }

        public virtual bool Deleted { get; set; }

        public virtual DateTime Created { get; set; }

        public virtual DateTime? FirstLogin { get; set; }

        public virtual DateTime? LatestLogin { get; set; }

        public virtual bool ForceLogout { get; set; }

        public virtual IList<Membership> Memberships { get; set; }

        public virtual string Fullname
        {
            get
            {
                string fullname = string.Empty;
                if (!string.IsNullOrEmpty(Firstname))
                {
                    fullname = Firstname;
                }
                if (!string.IsNullOrEmpty(Lastname))
                {
                    if (!string.IsNullOrEmpty(fullname))
                    {
                        fullname += " ";
                    }
                    fullname += Lastname;
                }
                return string.IsNullOrEmpty(fullname) ? Username : fullname;
            }
        }

        public virtual string PhonebookName
        {
            get
            {
                string phoneBookName = string.Empty;
                if (!string.IsNullOrEmpty(Lastname))
                {
                    phoneBookName = Lastname;
                }
                if (!string.IsNullOrEmpty(Firstname))
                {
                    if (!string.IsNullOrEmpty(phoneBookName))
                    {
                        phoneBookName += ", ";
                    }
                    phoneBookName += Firstname;
                }
                return phoneBookName;
            }
        }

        public virtual Membership GetMembership(int id)
        {
            return Memberships.FirstOrDefault(m => m.ID == id);
        }
    }
}